iT邦幫忙

2025 iThome 鐵人賽

DAY 23
0

首先你可以在VSCode的旁邊找到類似蟲蟲的圖示,點入後你可以找到Run and Debug的選項可以嘗試點選看看

https://ithelp.ithome.com.tw/upload/images/20250922/20178484eXKvPLOfPa.png
https://ithelp.ithome.com.tw/upload/images/20250922/20178484xwG6bbimZA.png
但很快的你會發現,點選完成後會出現這個提示,代表目前Cmake不支持Debug的形式

CMake does not support automatic debugging for this file

https://ithelp.ithome.com.tw/upload/images/20250922/20178484RPdcrAohky.png
這個提示告訴你,當前的 CMake 配置無法直接支持自動調試某些文件。這可能是由於多種原因造成的,例如 CMake 配置不完全或缺少必要的調試符號

為什麼會出現這個錯誤?

這個錯誤通常是因為還沒有在launch.json裡面設定相關的調試信息,所以可以點選 create a launch.json file 來設定相關的訊息,如果要設定相關訊息的話可以參考這個網站
Debug code with Visual Studio Code

https://ithelp.ithome.com.tw/upload/images/20250922/201784841FtGFTeiCA.png
https://ithelp.ithome.com.tw/upload/images/20250922/20178484oiNOQk8x3B.png
這邊提供一個簡單的樣板給大家做參考使用

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Dog meme Debug",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/main",  // 你的可執行文件
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",  // 使用 GDB 調試
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "miDebuggerPath": "/usr/bin/gdb",  // Linux 的 GDB 預設路徑,根據你的環境進行修改
            "preLaunchTask": "build c_proj",  // 編譯任務
            "miDebuggerArgs": "",
            "logging": {
                "moduleLoad": false,
                "trace": true,
                "engineLogging": false
            }
        }
    ]
}

設定完成後你會看到原本蟲蟲的地方多了一個選項,而選項名稱就是剛剛在launch.json那邊設定的 "name"
https://ithelp.ithome.com.tw/upload/images/20250922/20178484XQp0W6P79C.png
但是你按了 ▶ 符號後會發現跳出一個警示符號,代表你有一個動作沒有做了
另外在上面你可以看到一個 preLaunchTask ,但表編譯的task,所以等於說你的launch只會記錄一個入口點,而真正執行的地方會在另外一個地方,所以參考網站裡面的內容,你會發現有一個檔案叫做task.json

下次內容會介紹如何使用task.json


上一篇
[Day 22] [cmake] 深入了解Cmake指令與看懂CMakelist.txt
下一篇
[Day 24] 學會下Cmake make 命令編譯了? 那我們來學VScode 內建自動化編譯/除錯方法
系列文
30 天精通 C 語言建置與除錯:從 Makefile 到 CMake 跨平台實戰24
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言